//
//  FWPostWordController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/4/14.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWPostWordController.h"
#import "FWCustomTextView.h"
#import "FWToolBarView.h"

@interface FWPostWordController ()<UITextViewDelegate>
@property (weak, nonatomic) FWCustomTextView *textView;
@property (weak, nonatomic) FWToolBarView *toolBarView;
@end

@implementation FWPostWordController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupBarItem];
   
    [self addTextView];
    
    [self addToolTar];
}

- (void)setupBarItem{
    
    self.title = @"发表文字";
    NSMutableDictionary *titDict = [NSMutableDictionary dictionary];
    titDict[NSFontAttributeName] = [UIFont systemFontOfSize:20 weight:0.2];
    [self.navigationController.navigationBar setTitleTextAttributes:titDict];
    
    NSMutableDictionary *leftDict = [NSMutableDictionary dictionary];
    leftDict[NSForegroundColorAttributeName] = [UIColor blackColor];
    leftDict[NSFontAttributeName] = [UIFont systemFontOfSize:17 weight:0.01];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(onClickCancel)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:leftDict forState:UIControlStateNormal];
    
    NSMutableDictionary *rightDict2 = [NSMutableDictionary dictionary];
    rightDict2[NSForegroundColorAttributeName] = MyColor(237, 153, 149);
    rightDict2[NSFontAttributeName] = [UIFont systemFontOfSize:17 weight:0.01];
    
    NSMutableDictionary *rightDict = [NSMutableDictionary dictionary];
    rightDict[NSForegroundColorAttributeName] = MyColor(218, 22, 19);
    rightDict[NSFontAttributeName] = [UIFont systemFontOfSize:17 weight:0.01];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStyleDone target:self action:@selector(postWord)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:rightDict forState:UIControlStateNormal];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:rightDict2 forState:UIControlStateDisabled];
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
    [self.navigationController.navigationBar layoutIfNeeded];
}

- (void)addTextView{
    FWCustomTextView *textView = [[FWCustomTextView alloc] init];
    textView.frame = self.view.bounds;
    textView.placeholder = @"这里添加文字，请勿发布色情、政治等违反国家法律的内容，违者封号处理。";
    textView.font = [UIFont systemFontOfSize:15];
    textView.delegate = self;
    [textView becomeFirstResponder];
    [self.view addSubview:textView];
    _textView = textView;
}

- (void)addToolTar{
    FWToolBarView *toolBarView = [FWToolBarView toolBarView];
    toolBarView.x = 0;
    toolBarView.width = self.view.width;
    toolBarView.y = self.view.height - toolBarView.height;
    [self.view addSubview:toolBarView];
    self.toolBarView = toolBarView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWikkChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyBoardWikkChangeFrame:(NSNotification *)notification{
    CGRect keyboardF = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.toolBarView.transform = CGAffineTransformMakeTranslation(0, keyboardF.origin.y - kScreenH);
    }];
}

- (void)textViewDidChange:(UITextView *)textView{
    self.navigationItem.rightBarButtonItem.enabled = [textView hasText];
}

- (void)postWord{
    [self onClickCancel];
}

- (void)onClickCancel{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
